<template>
  <div class="container">
    <nav-bar
      title="养老机构区域分布情况"
    ></nav-bar>
    <div class="search-box">
      <div class="left-box">
        <pub-area
          :maxSelectLevel="3"
          @change="changeArea"
          customRightIcon
          label=""
          :isLink="false"
          valueIsLast
          ></pub-area>
      </div>
    </div>
    <ul class="content-box">
      <li v-for="(item, i) in state.info" :key="i">
        <div class="top-text">
          <div class="label">{{ item.name }}</div>
          <div class="value">
            <count-to
              :startVal="0"
              :endVal="Number(item.num)"
              :decimals="0"
              :duration="1500"
            ></count-to>
          </div>
        </div>
        <div class="bottom-line">
          <div
            class="inner-line"
            :style="{ width: (item.num / maxNum) * 100 + '%' }"
          ></div>
        </div>
      </li>
    </ul>
  </div>
</template>

<script setup>
import { useUserStore } from '@/pinia/modules/user'
import { ref, reactive, inject, onBeforeMount } from 'vue'
import { CountTo } from 'vue3-count-to'
import { get_pension_area } from '@/api/elderDistribution'
const changeComponents = inject('changeComponents')

const state = reactive({
  form: {
    areaCode: useUserStore().userLastArea
  },
  info: []
})
const maxNum = ref(0)

const back = () => {
  changeComponents('view')
}

const changeArea = val => {
  state.form.areaCode = val
  getAreaData()
}

const getAreaData = async () => {
  let res = await get_pension_area(state.form)
  if (res.succeed) {
    let numArr = []
    res.data.forEach(element => {
      numArr.push(Number(element.num))
    })
    state.info = res.data
    setTimeout(() => {
      maxNum.value = Math.max.apply(null, numArr)
    }, 500)
  }
}

onBeforeMount(() => {
  getAreaData()
})
</script>
<style lang="less" scoped>
.container {
  height: 100%;
  .search-box {
    display: flex;
    height: 90px;
    width: 100%;
    border-bottom: 1px solid #e6e6e6;
    justify-content: space-between;
    background-color: #fff;
    .left-box {
      height: 100%;
      :deep(.van-cell) {
        width: 220px;
        height: 100%;
        input {
          // max-width: 400px;
        }
      }
    }
    .right-box {
      height: 100%;
      margin-right: 40px;
      .text {
        line-height: 90px;
        font-size: 30px;
        font-weight: 400;
        color: #4375e4;
      }
    }
  }
  .content-box {
    background-color: #fff;
    padding: 30px 40px;
    overflow: auto;
    height: calc(100% - 240px);
    li {
      margin: 0px 0 40px 0;
      .top-text {
        display: flex;
        justify-content: space-between;
        .label {
          font-size: 30px;
        }
        .value {
          font-size: 30px;
          font-family: Microsoft YaHei;
        }
      }
      .bottom-line {
        margin-top: 20px;
        width: 100%;
        height: 10px;
        border-radius: 5px;
        .inner-line {
          width: 0%;
          transition: 1000ms;
          height: 100%;
          border-radius: 5px;
          background-color: #2588ff;
        }
      }
    }
  }
}
</style>
